import usePermissionStore from '@/store/modules/permission';
import useUserStore from '@/store/modules/user.js';
/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkPermi(value) {
  if (value && value instanceof Array && value.length > 0) {
    const permissions = usePermissionStore().selfPermissions;
    const permissionDatas = value;
    const all_permission = '*:*:*';

    const hasPermission = permissions.some(permission => {
      return (
        all_permission === permission || permissionDatas.includes(permission)
      );
    });
    if (!hasPermission) {
      return false;
    }
    return true;
  } else {
    console.error(
      `need roles! Like checkPermi="['system:user:add','system:user:edit']"`
    );
    return false;
  }
}

export function slice(str, start, end) {
  return str.slice(start, end);
}

export function clearForm(form) {
  if (Object.keys(form).length == 0) return;
  form.clearValidate();
}
//数组转字符串
export function arrTurnStr(arr, type) {
  if (!arr) return;
  let list = [];
  if (type) {
    if (type == 'item') {
      list = arr.join(';');
    }
  } else {
    list = arr
      ?.map(item => {
        return item.name;
      })
      .join(';');
  }

  return list;
}

export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone');
  }
  const targetObj = source.constructor === Array ? [] : {};
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys]);
    } else {
      targetObj[keys] = source[keys];
    }
  });
  return targetObj;
}

export function formatKey(arr) {
  arr?.forEach((item, index) => {
    item['key'] = index;
    item['visible'] = true;
  });
}

export function openUrl(url) {
  window.open(url);
}

export function checkBtn(arr) {
  let list = [];
  usePermissionStore().selfPermissions?.map(item => {
    //是否是管理员
    if (usePermissionStore().selfPermissions?.includes('*:*:*')) {
      if (item == '*:*:*') return;
      if (list.length) return;
      list = arr?.map(btn => {
        return btn?.split(':')[1];
      });
    } else {
      if (arr?.includes(item)) {
        list.push(item.split(':')[1]);
      }
    }
  });
  return list;
}

export function removeFileHead(path) {
  if (!path) return '';
  let beseApi = import.meta.env.VITE_APP_BASE_API;
  if (path.includes(beseApi)) {
    return path.replace(beseApi, '');
  } else {
    return path;
  }
}

export function addFileHead(path) {
  if (!path) return '';
  let beseApi = import.meta.env.VITE_APP_BASE_API;
  if (path.includes(beseApi)) {
    return path;
  } else {
    return beseApi + path;
  }
}

export function filterUserName(id) {
  if (!id) return;
  let item = useUserStore().allUserList?.find(item => item.ID == id);
  return item?.Name;
}

export function filterSuffix(name, suffix) {
  if (!name) return '';
  if (name.includes(suffix)) {
    return name;
  } else {
    return name + suffix;
  }
}

export function log(val) {
  console.log(val);
}
